package com.manager.plat.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.manager.plat.dao.IListWhiteDao;
import com.manager.plat.util.BaseUtil;
import com.manager.plat.vo.ListWhiteVo;
/**
 * 白名单数据实现
 * @author liugeng
 *
 */
public class ListWhiteDaoImpl extends JdbcDaoSupport implements IListWhiteDao {
	private static Logger logger = Logger.getLogger(ListWhiteDaoImpl.class);
	
	/**获得白名单列表*/
	@SuppressWarnings("unchecked")
	@Override
	public List<ListWhiteVo> getListWhiteList() {
		String sql = "select id, mobile, sp_code from tb_ivr_list_white where status = 1";
		logger.info(String.format("获得白名单列表[%s]", sql));
		
		List<ListWhiteVo> listWhiteVoList = this.getJdbcTemplate().query(sql, rowMapper);
		if(listWhiteVoList != null && listWhiteVoList.size() > 0){
			return listWhiteVoList;
		}
		return null;
	}
	/**根据ID获得白名单信息*/
	@SuppressWarnings("unchecked")
	@Override
	public ListWhiteVo getListWhiteVoById(int id) {
		String sql = "select id, mobile, sp_code from tb_ivr_list_white where id = ? and status = 1";
		Object[] param = new Object[]{id};
		logger.info(String.format("根据ID获得白名单信息[%s]", BaseUtil.logSQL(sql, param)));
		
		List<ListWhiteVo> listWhiteVoList = this.getJdbcTemplate().query(sql, param, rowMapper);
		if(listWhiteVoList != null && listWhiteVoList.size() > 0){
			return listWhiteVoList.get(0);
		}
		return null;
	}
	
	private ParameterizedRowMapper<ListWhiteVo> rowMapper = new ParameterizedRowMapper<ListWhiteVo>() {
		@Override
		public ListWhiteVo mapRow(ResultSet rs, int rowNum) throws SQLException {
			ListWhiteVo listWhiteVo = new ListWhiteVo();
			listWhiteVo.setId(rs.getInt("id"));
			listWhiteVo.setMobile(rs.getString("mobile"));
			listWhiteVo.setSp_code(rs.getString("sp_code"));
			return listWhiteVo;
		}
	};
	/**新增白名单信息*/
	@Override
	public int batchInsertListWhite(List<ListWhiteVo> listWhiteList) {
		String sql = "insert into tb_ivr_list_white (mobile, sp_code, status) values(?,?,1)";
		int[] count = this.getJdbcTemplate().batchUpdate(sql, new autoInsertListWhitePsSetter(listWhiteList));
		if(count.length > 0){
			return count.length;
		}
		return 0;
	}
	
	private class autoInsertListWhitePsSetter implements BatchPreparedStatementSetter{
		final List<ListWhiteVo> listWhiteListTemp;
		public autoInsertListWhitePsSetter(List<ListWhiteVo> listWhiteList){
			listWhiteListTemp = listWhiteList;
		}
		
		@Override
		public void setValues(PreparedStatement ps, int i) throws SQLException {
			ListWhiteVo listWhiteVo = listWhiteListTemp.get(i);
			ps.setString(1, listWhiteVo.getMobile());
			ps.setString(2, listWhiteVo.getSp_code());
		}
		@Override
		public int getBatchSize() {
			return listWhiteListTemp.size();
		}
	}
	
	/**修改白名单信息*/
	@Override
	public int updateListWhite(ListWhiteVo listWhiteVo) {
		String sql = "update tb_ivr_list_white set mobile = ?, sp_code = ? where id = ?";
		Object[] param = new Object[]{listWhiteVo.getMobile(),listWhiteVo.getSp_code(),listWhiteVo.getId()};
		logger.info(String.format("修改白名单信息[%s]", BaseUtil.logSQL(sql, param)));
		
		int count = this.getJdbcTemplate().update(sql, param);
		return count;
	}
	/**删除白名单信息*/
	@Override
	public int deleteListWhite(int id) {
		String sql = "update tb_ivr_list_white set status = 0 where id = ?";
		Object[] param = new Object[]{id};
		logger.info(String.format("删除白名单信息[%s]", BaseUtil.logSQL(sql, param)));
		
		int count = this.getJdbcTemplate().update(sql, param);
		return count;
	}
}
